package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class JieMain2 extends AppCompatActivity {

    private TextView tWendu;

    private TextView tShidu;

    private TextView tShengshi;

    private TextView tFengsu;


    private TextView tWeather;
    private ImageView imgTianqiJpg;
    private TextView tTitle;

    private TextView tUpdatatime;
    private TextView txtCity;
    private Button b_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jie_main4);
        tWendu = (TextView) findViewById(R.id.t_wendu);

        tShidu = (TextView) findViewById(R.id.t_shidu);

        tShengshi = (TextView) findViewById(R.id.t_shengshi);

        tFengsu = (TextView) findViewById(R.id.t_fengsu);


        tWeather = (TextView) findViewById(R.id.t_weather);

        imgTianqiJpg = (ImageView) findViewById(R.id.img_tianqi_jpg);
        tUpdatatime = (TextView) findViewById(R.id.t_updatatime);
        txtCity = (TextView) findViewById(R.id.txt_city);

        tTitle = (TextView) findViewById(R.id.t_title);
        b_main=(Button)findViewById(R.id.b_main) ;

       Weather weather=new Weather();
        try{

            weather.execute(new URL("https://api.help.bj.cn/apis/weather/?id=101210401"));
        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }
        b_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(JieMain2.this, MainActivity.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
                startActivity(intent);
            }
        });
    }



    public void mainweather(View view) {
    }
    class Weather extends AsyncTask<URL,Integer,String> {
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            WeatherBean weatherBean= JSON.parseObject(s,WeatherBean.class);

            txtCity.setText(weatherBean.getCity());
            tUpdatatime.setText(weatherBean.getUptime());
            tTitle.setText(weatherBean.getToday());
            tWendu.setText(weatherBean.getTemp()+"   ℃");
            tShidu.setText(weatherBean.getHumidity());
            tShengshi.setText(weatherBean.getCity());
            tFengsu.setText(weatherBean.getWdspd());
            tWeather.setText(weatherBean.getWeather());
            //将天气文字转换为图片
            if(weatherBean.getWeather()!=null){
                if(weatherBean.getWeather().equals("晴")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.qing);
                }
                if(weatherBean.getWeather().equals("多云")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.duoyun);
                }
                if(weatherBean.getWeather().equals("阴")){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.ying);
                }
                //判断天气情况中是否含有该关键字
                if(weatherBean.getWeather().indexOf("雨")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.yu);
                }
                if(weatherBean.getWeather().indexOf("霾")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.mai);
                }
                if(weatherBean.getWeather().indexOf("雾")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.wu);
                }
                if(weatherBean.getWeather().indexOf("雪")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.xue);
                }
            }else {
                Log.d("MainActivity","未找到该天气");
                imgTianqiJpg.setBackgroundResource(R.drawable.yuanshi);
            }



        }

        @Override
        protected String doInBackground(URL... urls) {
            String s=HttpGetData(urls[0]);
            Log.d("MainActivity","获得天气数据"+s);

            return s;
        }
    }

    //这个方法将执行
    public String HttpGetData(URL url){
        String s=null;
        try{  //1。网络连接创建
            HttpURLConnection httpURLConnection=  (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.connect();
            //2.发起连接
            if(httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                s =bufferedReader.readLine();
            }}
        catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }
}
